/*
 * Project Porcupine
 * Copyright (C) 2012  Team Porcupine
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
*
* @author Martin "quill18" Glaude <quill18@towerdive.com>
*/

package com.towerdive.porcupine;

import com.jme3.bullet.control.CharacterControl;
import com.jme3.input.controls.ActionListener;
import com.jme3.math.Vector3f;
import com.jme3.scene.CameraNode;
import com.jme3.scene.Node;

public class KeyboardHandler implements ActionListener {
    
    Node playerNode;
    
    boolean left, right, up, down;
    
    Vector3f baseCameraPosition;
    float cameraZoom = 0;
    float cameraZoomSpeed = 50;
    
    float playerSpeed = 0.15f;
    
    public KeyboardHandler(Node player_node) {
        this.playerNode = player_node;
        CameraNode camNode = (CameraNode) playerNode.getChild("Camera Node");
        baseCameraPosition = camNode.getLocalTranslation().clone();
    }

    /*public void onAnalog(String name, float value, float tpf) {
    }*/

    public void onAction(String name, boolean isPressed, float tpf) {
        if (name.equals("moveForward")) {
            up = isPressed;
        }
        if (name.equals("moveBackward")) {
            down = isPressed;
        }
        if (name.equals("moveRight")) {
            right = isPressed;
        }
        if (name.equals("moveLeft")) {
            left = isPressed;
        }
        
        if (name.equals("zoomIn") && isPressed) {
            zoomCamera(-tpf*cameraZoomSpeed);
        }
        if (name.equals("zoomOut") && isPressed) {
            zoomCamera(tpf*cameraZoomSpeed);
        }
        updateWalkDirection(tpf);
    }
    
    private void zoomCamera(float amount) {
        CameraNode camNode = (CameraNode) playerNode.getChild("Camera Node");
        cameraZoom -= amount;
        if(cameraZoom > 10) {
            cameraZoom = 10;
        }
        if(cameraZoom < 0) {
            cameraZoom = 0;
        }
        camNode.setLocalTranslation(baseCameraPosition.add(new Vector3f (0, -cameraZoom, cameraZoom)));
        camNode.lookAt(playerNode.getLocalTranslation(), Vector3f.UNIT_Y);
    }
    
    private void updateWalkDirection(float tpf) {
        CharacterControl player_control = (CharacterControl) playerNode.getControl(CharacterControl.class);
        Vector3f walkDirection = new Vector3f(0, 0, 0);
        
        if(player_control.onGround()) {
            if (up) {
                walkDirection.addLocal(new Vector3f(0,0,-1));
            }
            if (down) {
                walkDirection.addLocal(new Vector3f(0,0,1));
            }
            if (right) {
                walkDirection.addLocal(new Vector3f(1,0,0));
            }
            if (left) {
                walkDirection.addLocal(new Vector3f(-1,0,0));
            }
            
            /**
             * We want to set the total speed here, to make sure
             * diagonals aren't faster than they should be.
             */
            if(walkDirection.length() > 0) {
                walkDirection.normalizeLocal();
                walkDirection.multLocal(playerSpeed);
            }
            
            player_control.setWalkDirection(walkDirection);
        }
    }
}
